<template>
	<view class="container">
		<view class="top">
			<view class="top-left">
				<image v-if="user" class="img" :src="addBase(user.avatar)"></image>
			</view>
			<view class="top-right">
				<p class="tagp">
					<uni-tag :text="user.role" type="error" />
					<uni-tag class="tag" :text="user.username" type="primary" />
				</p>
				<p class="tagp">
					<uni-tag :text="'BMI ' + BMI" type="success" />
				</p>
			</view>
		</view>
		<view class="line">
			<view class="circle" @click="change">
				<p>修改信息</p>
			</view>
			<view class="circle" @click="logout">
				<p>切换账号</p>
			</view>
		</view>
		<view class="section-title">
			<p>预约记录</p>
			<view class="divider"></view>
		</view>
		<view class="appointment-list">
			<view class="appointment-item" v-for="(item, index) in list" :key="index">
				<view class="appointment-info">
					<p class="appointment-date">预约时间：{{ new Date(item.date).toLocaleString() }}</p>
				</view>
				<view class="appointment-actions">
					<view class="appointment-action">
						<text v-if="user.role === '学员'" class="appointment-action-text"
							@click="toDetail(item.trainerId)">查看教练</text>
						<text v-else>学员编号：{{ item.userId }}</text>
					</view>
					<view v-if="user.role === '学员'" class="appointment-action">
						<text class=" appointment-action-text" @click="cancelAppointment(item.id)">取消预约</text>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
import {
	addBase
} from '../../utils/myFunction';
import request from '../../utils/request';
export default {
	data() {
		return {
			user: null,
			BMI: 0,
			list: []
		};
	},
	onLoad() {
		this.user = uni.getStorageSync('user')
		this.BMI = parseFloat(this.user.weight / ((this.user.height / 100) * (this.user.height / 100))).toFixed(2);
	},
	onShow() {
		this.getList();
	},
	methods: {
		change() {
			uni.navigateTo({
				url: '/pages/mine/change'
			})
		},
		logout() {
			uni.reLaunch({
				url: '/pages/login/login'
			})
		},
		toDetail(id) {
			uni.navigateTo({
				url: `/pages/index/detail?id=${id}`
			})
		},
		getList() {
			if (this.user.role === '学员') {
				request({
					url: 'system/appointment/list',
					method: 'GET',
					data: {
						userId: this.user.id
					}
				}).then(res => {
					this.list = res.rows
				})
			} else {
				request({
					url: 'system/appointment/list',
					method: 'GET',
					data: {
						trainerId: this.user.id
					}
				}).then(res => {
					this.list = res.rows
				})
			}
		},
		addBase(path) {
			return addBase(path)
		},
		cancelAppointment(id) {
			uni.showModal({
				title: '提示',
				content: '确定要取消预约吗？',
				success: (res) => {
					if (res.confirm) {
						request({
							url: 'system/appointment/' + id,
							method: 'DELETE'
						}).then(res => {
							uni.showToast({
								title: '取消成功',
								icon: 'success'
							});
							this.getList();
						}).catch(err => {
							uni.showToast({
								title: '取消失败',
								icon: 'error'
							});
						});
					}
				}
			});
		}
	}
}
</script>

<style lang="scss">
.container {
	max-width: 100%;
	padding: 0 16px;
	background-color: #f9f9f9;
}

.top {
	padding-top: 30upx;
	display: flex;
	align-items: center;
	margin-bottom: 20px;

	.top-left {
		margin-left: 16%;

		.img {
			border: 3px solid #fff;
			width: 100px;
			height: 100px;
			border-radius: 50%;
			object-fit: cover;
			box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
			transition: border-color 0.3s ease;
		}

		.img:hover {
			border-color: #888;
		}
	}

	.top-right {
		margin-left: 30px;

		.tagp {
			margin-top: 10px;

			.tag {
				margin-left: 5px;
			}
		}
	}
}

.line {
	margin-top: 20px;
	display: flex;
	justify-content: space-around;

	.circle {
		width: 40%;
		height: 40px;
		border: #ccc solid 1px;
		border-radius: 20px;
		background-color: #fff;
		box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
		transition: transform 0.3s ease;

		p {
			text-align: center;
			font-weight: bold;
			line-height: 40px;
		}
	}

	.circle:hover {
		transform: translateY(-4px);
	}
}

.section-title {
	margin-top: 20px;
	margin-bottom: 10px;
	text-align: center;
	font-size: 18px;
	font-weight: bold;
	color: #333;
	position: relative;
}

.divider {
	width: 60px;
	height: 6px;
	background-color: #ccc;
	margin: 8px auto 0;
	border-radius: 3px;
}

.appointment-list {
	margin-top: 20px;
}

.appointment-item {
	background-color: #fff;
	border-radius: 8px;
	box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
	margin-bottom: 16px;
	padding: 16px;
	transition: transform 0.3s ease;
	display: flex;
	flex-direction: column;
}

.appointment-item:hover {
	transform: translateY(-4px);
}

.appointment-info {
	margin-bottom: 8px;
	display: flex;
	justify-content: space-between;
	align-items: center;
}

.appointment-date {
	font-size: 14px;
	color: #333;
	font-weight: bold;
}

.appointment-actions {
	display: flex;
	justify-content: space-between;
	margin-top: 8px;
}

.appointment-action {
	padding: 8px 12px;
	background-color: #f0f0f0;
	border-radius: 4px;
	transition: background-color 0.3s ease;
	cursor: pointer;

	&:last-child {
		background-color: #ffe4e4;

		.appointment-action-text {
			color: #ff4d4f;
		}
	}
}

.appointment-action:hover {
	background-color: #e0e0e0;
}

.appointment-action-text {
	font-size: 12px;
	color: #666;
}
</style>